package com.hsgene.consumables.service.impl;

import com.hsgene.common.DeleteFlagType;
import com.hsgene.common.util.basic.UUID32;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.support.Pagination;
import com.hsgene.consumables.condition.PurchaseConditon;
import com.hsgene.consumables.domain.GeneticGoodsPurchase;
import com.hsgene.consumables.domain.GeneticGoodsPurchaseDetail;
import com.hsgene.consumables.dto.ConsumablesInventoryInfoDto;
import com.hsgene.consumables.dto.ConsumablesSelectListInfoDto;
import com.hsgene.consumables.dto.GeneticGoodsPurchaseDto;
import com.hsgene.consumables.persistence.ConsumablesInventoryRepository;
import com.hsgene.consumables.persistence.ConsumablesReceiptRepository;
import com.hsgene.consumables.persistence.GeneticGoodsPurchaseDetailRepository;
import com.hsgene.consumables.service.ConsumablesReceiptService;
import com.hsgene.consumables.service.api.GeneratorService;
import com.hsgene.generator.dto.GeneratorDto;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 入库service
 *
 * @author wxf
 * @date 2018/8/16 16:32
 **/
@Service
public class ConsumablesReceiptServiceImpl implements ConsumablesReceiptService {

    @Resource
    private ConsumablesReceiptRepository receiptRepository;

    @Resource
    private GeneticGoodsPurchaseDetailRepository detailRepository;

    @Resource
    private GeneratorService generatorService;

    @Resource
    private ConsumablesInventoryRepository inventoryRepository;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    public int addConsumablesReceiptInfo(List<GeneticGoodsPurchase> receiptInfos, String purchaseMember,
                                         String remarks) {
        boolean b = receiptInfos.stream().anyMatch(c -> c.getPurchaseAmount() > 9999);
        if(b){
           return -2;
        }
        //入库详情
       List<GeneticGoodsPurchaseDetail> purchaseDetails=new ArrayList<>();
        //编号
        ApiResult<GeneratorDto> hcrk = generatorService.numberGenerator("HCRK", null, null);
        for (GeneticGoodsPurchase c : receiptInfos) {
           ApiResult<GeneratorDto> generatorResult1 = generatorService.generate("default", null);
            if (generatorResult1.isSucc()) {
                c.setId(generatorResult1.getData().getSerialNumber());
            }
            c.setConvertAmount(c.getPurchaseAmount() * c.getSpecification());
            c.setPurchaseNo(hcrk.getData().getSerialNumber());
            c.setPurchaseMember(purchaseMember);
            c.setDeleteFlag(DeleteFlagType.NOT_DELETED.getCode());
            c.setPurchaseDateTime(new Date());
            c.setRemarks(remarks);
            ApiResult<GeneratorDto> generatorResult = generatorService.generate("genetic_goods_purchase_detail", null);
            if (generatorResult.isSucc()) {
                //构建PurchaseDetail
                GeneticGoodsPurchaseDetail geneticGoodsPurchaseDetail = getGeneticGoodsPurchaseDetail(c, generatorResult);
                purchaseDetails.add(geneticGoodsPurchaseDetail);
            }
        }
        //批量插入入库表
        receiptRepository.insertPurchase(receiptInfos);
        //批量插入入库详情表
        detailRepository.insertPurchaseDetail(purchaseDetails);
        //入库量进入库存表更新
        return receiptRepository.updateInventoryInfo(receiptInfos);
    }

    private GeneticGoodsPurchaseDetail getGeneticGoodsPurchaseDetail(GeneticGoodsPurchase c, ApiResult<GeneratorDto> generatorResult) {
        GeneticGoodsPurchaseDetail geneticGoodsPurchaseDetail = new GeneticGoodsPurchaseDetail();
        BeanUtils.copyProperties(c, geneticGoodsPurchaseDetail);
        geneticGoodsPurchaseDetail.setId(generatorResult.getData().getSerialNumber());
        geneticGoodsPurchaseDetail.setPurchaseId(c.getId());
        geneticGoodsPurchaseDetail.setCreateDateTime(new Date());
        geneticGoodsPurchaseDetail.setDeleteFlag(0);
        geneticGoodsPurchaseDetail.setGoodId(c.getGoodsId());
        geneticGoodsPurchaseDetail.setPurchaseAmount(c.getPurchaseAmount());
        geneticGoodsPurchaseDetail.setConverAmount(c.getConvertAmount());
        return geneticGoodsPurchaseDetail;
    }


    /**
     * 获取下拉列表
     *
     * @param page page
     * @return
     */
    @Override
    public List<ConsumablesSelectListInfoDto> getSelectInfo(int page) {
        return receiptRepository.getSelectListInfo((page - 1) * 10, 10);
    }

    /**
     * 获取入库记录列表
     *
     * @return
     */
    @Override
    public ApiResult<Pagination<GeneticGoodsPurchaseDto>> getReceiptRecordInfo(PurchaseConditon condition) {
        int totalCount =receiptRepository.getPurchaseCount(condition);
        Pagination<GeneticGoodsPurchaseDto> pagination = Pagination.newInstance(condition.getLimitHelper(), totalCount);
        if(totalCount>0){
            int rowNumber = (condition.getLimitHelper().getPage() - 1) * condition.getLimitHelper().getSize();
            List<GeneticGoodsPurchaseDto> list=receiptRepository.getReceiptList(condition);
            for(int i=0,n=list.size();i<n;i++){
                GeneticGoodsPurchaseDto geneticGoodsPurchaseDto = list.get(i);
                geneticGoodsPurchaseDto.setNumber(rowNumber + i + 1);
            }
            pagination.setResult(list);
        }else{
            pagination.setResult(new ArrayList<>());
        }
        return ApiResult.succ(pagination);
    }

    /**
     * 获取入库详情
     *
     * @param purchaseNo
     * @return
     */
    @Override
    public List<GeneticGoodsPurchaseDto> getReceiptDetailInfo(String purchaseNo) {
        return receiptRepository.getReceiptDetailInfo(purchaseNo);
    }

    /**
     * 商品库存
     *
     * @param goodsId 商品id
     * @return
     */
    @Override
    public List<ConsumablesInventoryInfoDto> getInventoryInfoByGoodsId(String goodsId) {
        return receiptRepository.getInventoryInfoByGoodsId(StringUtils.split(goodsId, ","));
    }
}
